
package com.example.liang.basamovie.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.liang.basamovie.R;
import com.example.liang.basamovie.adapter.RecommendAdapter;
import com.example.liang.basamovie.json.RecommendJson;
import com.example.liang.basamovie.json.RecommendJson2;
import com.example.liang.basamovie.utils.Path;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;


import java.util.List;


public class HomeFragment extends Fragment {
    RecyclerView recyclerView;
    List<Object> data;
    List<Object> data2;
    RecommendAdapter recommendAdapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, null);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview_recommend);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        RecyclerView.LayoutManager manager = new LinearLayoutManager(getContext(),
                LinearLayoutManager.VERTICAL,
                false);
        recyclerView.setLayoutManager(manager);
        getData();
    }

    private void getData() {
        OkHttpUtils.get()
                .url(Path.Recommend_PATH)
                .build()
                .execute(new StringCallback(){
                    @Override
                    public void onError(Request request, Exception e){
                        Log.e("info","-------推荐碎片数据下载失败--------");
                    }
                    @Override
                    public void onResponse(String response){
                        data = RecommendJson.getRecommend(response);
                        data2= RecommendJson2.getRecommend2(response);
                        Log.e("info","推荐碎片数据下载成功，大小为:------"+data.size());
                        recommendAdapter=new RecommendAdapter(getContext(),data,data2);
                        recyclerView.setAdapter(recommendAdapter);
                    }
                });
    }

    }


